
package com.doc2video.controller;

import com.doc2video.common.Result;
import com.doc2video.dto.UserLoginDTO;
import com.doc2video.dto.UserRegisterDTO;
import com.doc2video.entity.User;
import com.doc2video.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户Controller
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<String> register(@Validated @RequestBody UserRegisterDTO registerDTO) {
        return userService.register(registerDTO);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<String> login(@Validated @RequestBody UserLoginDTO loginDTO, HttpServletRequest request) {
        return userService.login(loginDTO, request);
    }

    @ApiOperation("用户退出")
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
        return userService.logout(request);
    }

    @ApiOperation("获取当前用户信息")
    @GetMapping("/info")
    public Result<User> getCurrentUser(HttpServletRequest request) {
        return userService.getCurrentUser(request);
    }

    @ApiOperation("更新用户信息")
    @PutMapping("/info")
    public Result<String> updateUserInfo(@RequestBody User user, HttpServletRequest request) {
        return userService.updateUserInfo(user, request);
    }

    @ApiOperation("修改密码")
    @PutMapping("/password")
    public Result<String> changePassword(@RequestParam String oldPassword, 
                                       @RequestParam String newPassword, 
                                       HttpServletRequest request) {
        return userService.changePassword(oldPassword, newPassword, request);
    }
}
